package com.linzhaowei.headhuntingservice;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.linzhaowei.headhuntingservice.bean.JobInformation;
import com.linzhaowei.headhuntingservice.bean.JobObjective;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.io.IOException;

public class JobInformationAdd extends AppCompatActivity implements View.OnClickListener {

    EditText name;
    EditText address;
    EditText salary;
    EditText information;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_information_update);
        initViews();
        setEvents();

    }

    private void initViews() {
        name = findViewById(R.id.j_i_u_name);
        address = findViewById(R.id.j_i_u_address);
        salary = findViewById(R.id.j_i_u_salary);
        information = findViewById(R.id.j_i_u_information);
        button = findViewById(R.id.j_i_u_btn);
    }

    private void setEvents() {
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.j_i_u_btn:
                update();
                break;
            default:
                break;
        }
    }


    private void update() {
        SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
        JobInformation jobInformation = new JobInformation();
        jobInformation.setUser_id(sharedPreferences.getInt("id", -1));
        jobInformation.setName(name.getText().toString().trim());
        jobInformation.setAddress(address.getText().toString().trim());
        jobInformation.setSalary(Integer.valueOf(salary.getText().toString()));
        jobInformation.setInformation(information.getText().toString());
        String string = JSON.toJSONString(jobInformation);
        new Thread(() -> {
            HttpUtils httpUtils = new HttpUtils();
            try {
                httpUtils.post(Ip.ip + "/user/add_job_information", string);
                runOnUiThread(() -> Toast.makeText(JobInformationAdd.this, "添加成功", Toast.LENGTH_SHORT).show());
                finish();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
